<?php

/**
 * 
 * @project     : innoTweets 1.0
 * @author      : Dino (DBK)
 * 
 * @package     : Tweets
 * @description : List of tweets
 * 
 */
if (!defined('BASEPATH')) {
  exit('No direct script access allowed');
}

class Tweets extends AUTH_Controller {

  public function index($type = 'active') {

    // Setting limit count
    $tweet_limit = $this->input->get('limit', TRUE);
    $tweet_limit = (int) (!empty($tweet_limit) && is_numeric($tweet_limit)) ? $tweet_limit : 10;

    // Search term
    $search_term = $this->sanitize($this->input->get('search_term', TRUE));

    // Getting selected list
    if ($type == 'top') {
      $get_tweets = $this->tweet_model->get_top_tweets($tweet_limit);
      $get_tweets_count = $this->tweet_model->get_top_tweets_count(NULL);
      $tweets = $get_tweets['result'];
    } elseif ($type == 'my') {
      $get_tweets = $this->tweet_model->get_my_tweets($this->current_user['user_id'], $tweet_limit);
      $get_tweets_count = $this->tweet_model->get_my_tweets_count($this->current_user['user_id'], NULL);
      $tweets = $get_tweets['result'];
    } elseif ($type == 'new') {
      $get_tweets = $this->tweet_model->get_tweets($tweet_limit);
      $get_tweets_count = $this->tweet_model->get_tweets_count(NULL);
      $tweets = $get_tweets['result'];
    } elseif ($type == 'search' && !empty($search_term)) {
      $get_tweets = $this->tweet_model->search_tweets($search_term, $tweet_limit);
      $get_tweets_count = $this->tweet_model->search_tweets_count($search_term, NULL);
      $tweets = $get_tweets['result'];
    } else {
      $type = 'active';
      $get_tweets = $this->tweet_model->get_active_tweets($tweet_limit);
      $get_tweets_count = $this->tweet_model->get_active_tweets_count(NULL);
      $tweets = $get_tweets['result'];
    }

    // Getting additional infos
    $get_all_tweet_users = $this->tweet_model->get_all_tweet_users();
    $all_tweet_users = $get_all_tweet_users['result'];
    $get_all_tweet_extras = $this->tweet_model->get_all_tweet_extras($this->current_user['user_id']);
    $all_tweet_extras = $get_all_tweet_extras['result'];

    // Getting all non users
    $get_all_users = $this->user_model->get_all_non_users();
    $non_users = $get_all_users['result'];

    // Preparing view data
    $view_data = array();
    $view_data['menu_active'] = 'tweets';
    $view_data['tweets'] = $tweets;
    $view_data['tweets_count'] = $get_tweets_count['count'];
    $view_data['type'] = $type;
    $view_data['all_tweet_users'] = $all_tweet_users;
    $view_data['all_tweet_extras'] = $all_tweet_extras;
    $view_data['tweet_limit'] = $tweet_limit;
    $view_data['search_term'] = $search_term;
    $view_data['non_users'] = $non_users;

    // Loading view
    $this->load_view('tweets', $view_data);
    return;
  }

}

?>